package com.example.musicplayer.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.musicplayer.R;
import com.example.musicplayer.adapter.MenuAdapter;
import com.example.musicplayer.fragment.MainTabFindFragment;
import com.example.musicplayer.fragment.MainTabMyFragment;
import com.example.musicplayer.fragment.MainTabVideoFragment;
import com.example.musicplayer.response.MenuResponse;
import com.example.musicplayer.setting.AboutmusicActivity;
import com.example.musicplayer.setting.TimestopActivity;
import com.example.musicplayer.square.Square;
import com.example.musicplayer.utils.Contanst;
import com.example.musicplayer.utils.GetAllActivity;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    ViewPager2 viewPager;
    TabLayout tabLayout;
    ImageView iv_main_menu;
    ConstraintLayout constlout_main_top;
    DrawerLayout drawerlout_main_top;

    private NavigationView navView;//导航视图


    private String[] title = {"我的", "发现"};
    private List<Fragment> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GetAllActivity.getInstance().addActivity(this);
        //获取组件
        viewPager = findViewById(R.id.vp2_main);
        tabLayout = findViewById(R.id.tablt_main);
        iv_main_menu = findViewById(R.id.iv_main_menu);
        constlout_main_top = findViewById(R.id.constlout_main_top);
        drawerlout_main_top = findViewById(R.id.drawerlout_main_top);
        navView = findViewById(R.id.nav_view);
        GetAllActivity.getInstance().addActivity(this);

        //获取头部视图
        View headerView = navView.getHeaderView(0);
        //头像点击
        headerView.findViewById(R.id.iv_avatar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoginPageActivity.class);
                startActivity(intent);
            }
        });
        //头部导航栏导航菜单点击
        navView.setNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.item_message:
                    Intent intent3 = new Intent(MainActivity.this, LoginPageActivity.class);
                    startActivity(intent3);
                    break;
                case R.id.item_space:
                    Intent intent1 = new Intent(MainActivity.this, Square.class);
                    Bundle bundle = this.getIntent().getExtras();
                    String manName = bundle.getString("username");
                    Bundle bund = new Bundle();
                    bund.putString("manname", manName);
                    bund.putString("manid", "00");
                    intent1.putExtras(bund);
                    startActivity(intent1);
                    break;
                case R.id.item_xiugai:
                    Intent intent4 = new Intent(MainActivity.this, ChangePasswordActivity.class);
                    startActivity(intent4);
                    break;
                case R.id.item_clock:
                    Intent intent2 = new Intent(MainActivity.this, TimestopActivity.class);
                    startActivity(intent2);
                    break;
                case R.id.item_about:
                    Intent intent5 = new Intent(MainActivity.this, AboutmusicActivity.class);
                    startActivity(intent5);
                    break;
                case R.id.item_setting:
                    Intent intent = new Intent(MainActivity.this, SettingActivity.class);
                    startActivity(intent);
                case R.id.item_quit:
                   GetAllActivity.getInstance().exit();
                    break;
                default:
                    break;
            }
            //关闭滑动菜单
            drawerlout_main_top.closeDrawer(GravityCompat.START);
            return true;
        });


        //绑定列表控件

        showMenuList();
        //初始化顶部导航
        initTopNav();

        //点击事件绑定
        iv_main_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerlout_main_top.openDrawer(GravityCompat.START);
            }
        });



    }




    private void initTopNav() {
        list.add(MainTabMyFragment.newInstance(title[0], "#03A9F4"));
        list.add(MainTabFindFragment.newInstance(title[1], "#8BC34A"));
        viewPager.setAdapter(new MainPagerAdapter(this));
        new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(title[position]);
            }
        }).attach();
    }
    private void showMenuList() {
        //解析JSON数据
        MenuResponse menuResponse = new Gson().fromJson(Contanst.JSON, MenuResponse.class);
        if (menuResponse.getCode() == Contanst.SUCCESS) {
            //为空处理
            if(menuResponse.getData() ==null){
                showMsg("返回菜单数据为空");
                return;
            }
            List<MenuResponse.DataBean> data = menuResponse.getData();
            //设置适配器的布局和数据源
            MenuAdapter menuAdapter = new MenuAdapter(R.layout.item_menu, data);
            //item点击事件
            menuAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                showMsg(data.get(position).getName());
                //关闭滑动菜单
                drawerlout_main_top.closeDrawer(GravityCompat.START);
            });
           } else {
            //错误提示
            showMsg(menuResponse.getMsg());
        }
    }
    /**
     * Toast提示
     *
     * @param msg 内容
     */
    private void showMsg(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public class MainPagerAdapter extends FragmentStateAdapter {

        public MainPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @Override
        public Fragment createFragment(int position) {
            return list.get(position);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

    }


}